package com.source.hotelbath.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.source.hotelbath.R;
import com.source.hotelbath.base.BizActivity;
import com.source.hotelbath.fragment.MineFragment;
import com.source.hotelbath.net.CustomerRequestConstants;
import com.source.hotelbath.utils.MyCookieStore;

import java.util.List;

import okhttp3.Cookie;
import okhttp3.HttpUrl;

/**
 * 忘记密码
 */

public class ForgetActivity extends BizActivity {

    private WebView web_about;
    private LinearLayout ll_header_left;
    private  LinearLayout ll_header_right;
    @Override
    protected void onBaseCreate(Bundle var1) {
        setContentView(R.layout.activity_forget);
    }

    @Override
    protected void bindView(Bundle var1) {
        ll_header_left = (LinearLayout) findViewById(R.id.ll_header_left);

        ll_header_right = (LinearLayout) findViewById(R.id.ll_header_right);
        ll_header_right.setVisibility(View.GONE);

        ll_header_left.setOnClickListener(this);
        TextView tv_header_title= (TextView) findViewById(R.id.tv_header_title);
        tv_header_title.setText("忘记密码");
        web_about = (WebView)findViewById(R.id.web_about);
        web_about.getSettings().setJavaScriptEnabled(true);
        web_about.loadUrl(CustomerRequestConstants.newUrl+CustomerRequestConstants.FORGET);
        web_about.setWebViewClient(new WebClient());

//        initCookie();
    }

    private void initCookie() {
        com.lzy.okgo.cookie.store.CookieStore cookieStore = OkGo.getInstance().getCookieJar().getCookieStore();
        //Urls.URL_METHOD
        HttpUrl httpUrl = HttpUrl.parse("http://test.subangchina.com:8030/zuxi/work/api/user/logon.ajax?phone=work&password=123456");
        List<Cookie> cookies = cookieStore.getCookie(httpUrl);
//        MyCookieStore.synCookies(getContext(),CustomerRequestConstants.newUrl+CustomerRequestConstants.MINE,cookies.toString());
        for (int i=0;i<cookies.size();i++){
            MyCookieStore.synCookies(this, CustomerRequestConstants.newUrl+CustomerRequestConstants.MINE,cookies.get(i).name()+"="+cookies.get(i).value());

        }
        web_about.loadUrl(CustomerRequestConstants.newUrl+CustomerRequestConstants.MINE);
        web_about.setWebViewClient(new WebClient());
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view==ll_header_left){
            finish();
        }
    }

    private class WebClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d("url",url);
            view.loadUrl(url);
//            if (url.contains("logout")) {
//                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
//                Intent intent = new Intent(getContext(), LoginActivity.class);
//                startActivity(intent);
//
//                getActivity().finish();
//
//            }
            return true;
        }

        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            //这里进行无网络或错误处理，具体可以根据errorCode的值进行判断，做跟详细的处理。
            Log.d("errorCode",errorCode+"");
        }

    }
}
